import { DEFAULT_PAGE_SIZE } from '../../constant/constant'
import { sleep } from '../util/timerUtil'

export class MockData {
  static async fetchPagedData (generator, params, { ttl = 200, total = 33 } = {}) {
    await sleep(ttl)
    const { pageSize = DEFAULT_PAGE_SIZE, pageNo = 1 } = params
    const offset = (pageNo - 1) * pageSize
    if (offset >= total) {
      return []
    }
    const results = []
    for (let i = offset; i < Math.min(offset + pageSize, total); i++) {
      results.push(generator({ id: i, ...params }))
    }
    return results
  }

  static async fetchOneData (generator, { ttl = 200 } = {}) {
    await sleep(ttl)
    return generator()
  }

  static async fetchData(generator, { ttl = 200, total = 10 } = {}){
    const results = []
    for (let i = 0; i < total; i++) {
      results.push(generator({ id: i + 1 }))
    }
    return results
  }
}